<template>
  <div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="visible"
      :before-close="handleCancel"
      width="40%"
    >
      <div class="formBox">
        <div class="formHead">{{ formTitle }}</div>
        <div class="formWhite">
          <div class="left"><span class="red">*</span>监测公司</div>
          <div class="right">
            <el-select
              v-model="formData.monitorId"
              size="small"
              placeholder="请选择"
              @change="handleMonitorChange"
              :disabled="isDetail"
            >
              <el-option
                v-for="item in monitorCompanyList"
                :key="item.monitorId"
                :label="item.monitorName"
                :value="item.monitorId"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="formGray">
          <div class="left"><span class="red">*</span>白蚁防治公司</div>
          <div class="right">
            <el-input
              v-model="formData.controlName"
              placeholder="请输入"
              size="small"
              :disabled="isDetail"
            ></el-input>
          </div>
        </div>
        <div class="formWhite">
          <div class="left"><span class="red">*</span>账号</div>
          <div class="right">
            <el-input
              v-model="formData.accountNumber"
              placeholder="请输入"
              size="small"
              :disabled="isDetail"
            ></el-input>
          </div>
        </div>
        <!-- <div class="formGray">
          <div class="left"><span class="red">*</span>密码</div>
          <div class="right">
            <el-input
              v-model="formData.accountPassword"
              placeholder="请输入"
              size="small"
              type="password"
            ></el-input>
          </div>
        </div> -->
        <div class="formGray">
          <div class="left">设备数量</div>
          <div class="right">
            <el-input
              v-model="formData.devicesNumber"
              placeholder="请输入"
              size="small"
              :disabled="true"
            ></el-input>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button
          v-if="!isDetail"
          type="primary"
          @click="handleConfirm"
          size="small"
          >确定</el-button
        >
        <el-button type="primary" plain @click="handleCancel" size="small">{{
          isDetail ? "关闭" : "取消"
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { queryMonitorCompanyData } from "@/api/manage/asset";

export default {
  name: "EditDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    formData: {
      type: Object,
      default: () => ({
        monitorId: "",
        monitorName: "",
        controlName: "",
        controlId: "",
        accountNumber: "",
        accountPassword: "",
        devicesNumber: "",
        id: "",
      }),
    },
    formTitle: {
      type: String,
      default: "编辑",
    },
    isDetail: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      monitorCompanyList: [],
    };
  },
  computed: {
    dialogTitle() {
      return "监测账号管理→" + this.formTitle;
    },
  },
  created() {
    this.getMonitorCompanyList();
  },
  methods: {
    // 获取监测公司列表
    getMonitorCompanyList() {
      queryMonitorCompanyData().then((res) => {
        if (res.errorCode === "000000") {
          this.monitorCompanyList = res.data;
        }
      });
    },
    // 监测公司选择变化
    handleMonitorChange(val) {
      const selectedCompany = this.monitorCompanyList.find(
        (item) => item.monitorId === val,
      );
      if (selectedCompany) {
        this.formData.monitorName = selectedCompany.monitorName;
      }
    },
    // 确定按钮
    handleConfirm() {
      // 表单验证
      if (!this.formData.monitorId) {
        this.$modal.msgWarning("请选择监测公司！");
        return;
      }
      if (!this.formData.controlName) {
        this.$modal.msgWarning("请填写白蚁防治公司！");
        return;
      }
      if (!this.formData.accountNumber) {
        this.$modal.msgWarning("请填写账号！");
        return;
      }
      if (!this.formData.accountPassword) {
        this.$modal.msgWarning("请填写密码！");
        return;
      }

      // 触发确认事件，传递表单数据
      this.$emit("confirm", this.formData);
    },
    // 取消按钮
    handleCancel() {
      this.$emit("cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
.formBox {
  .left {
    display: inline-block;
    height: 44px;
    line-height: 44px;
    width: 140px;
    padding-right: 15px;
    border-left: 1px solid #e4e4dc;
    border-right: 1px solid #e4e4dc;
    font-family: MicrosoftYaHei;
    font-size: 12px;
    color: #303133;
    text-align: right;
    vertical-align: top;
  }
  .right {
    display: inline-block;
    width: calc(100% - 140px);
    height: 44px;
    border-right: 1px solid #e4e4dc;
    padding: 6px 15px;
    .el-select {
      width: 100% !important;
    }
    .el-input {
      width: 100% !important;
    }
  }
  .formHead {
    width: 100%;
    height: 44px;
    background-color: #73879c;
    height: 44px;
    line-height: 44px;
    text-align: center;
    font-family: MicrosoftYaHei, MicrosoftYaHei;
    font-weight: bold;
    font-size: 12px;
    color: #ffffff;
  }
  .formWhite {
    width: 100%;
    height: 44px;
    background-color: #fff;
    border-bottom: 1px solid #e4e4dc;
  }
  .formGray {
    width: 100%;
    height: 44px;
    background-color: #eef0f2;
    border-bottom: 1px solid #e4e4dc;
  }
}

.red {
  color: red;
  margin-right: 2px;
  vertical-align: top;
}

::v-deep .el-dialog__header {
  border-bottom: 1px solid #e9e9ec;
}

::v-deep .el-dialog__footer {
  border-top: 1px solid #e9e9ec;
}
</style>
